<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - materials - canvas texture</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
		<style>
			#drawing-canvas {
				position: absolute;
				background-color: #000000;
				top: 0px;
				right: 0px;
				z-index: 3000;
				cursor: crosshair;
				touch-action: none;
			}
		</style>
	</head>
	<body>

		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - canvas as a texture
			<div>click and draw in the white box</div>
		</div>
		<canvas id="drawing-canvas" height="128" width="128"></canvas>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.module.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';

			let camera, scene, renderer, mesh, material;
			const drawStartPos = new THREE.Vector2();

			init();
			setupCanvasDrawing();

			function init() {

				camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.z = 500;

				scene = new THREE.Scene();

				material = new THREE.MeshBasicMaterial();

				mesh = new THREE.Mesh( new THREE.BoxGeometry( 200, 200, 200 ), material );
				scene.add( mesh );

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setAnimationLoop( animate );
				document.body.appendChild( renderer.domElement );

				window.addEventListener( 'resize', onWindowResize );

			}

			// Sets up the drawing canvas and adds it as the material map

			function setupCanvasDrawing() {

				// get canvas and context

				const drawingCanvas = document.getElementById( 'drawing-canvas' );
				const drawingContext = drawingCanvas.getContext( '2d' );

				// draw white background

				drawingContext.fillStyle = '#FFFFFF';
				drawingContext.fillRect( 0, 0, 128, 128 );

				// set canvas as material.map (this could be done to any map, bump, displacement etc.)

				material.map = new THREE.CanvasTexture( drawingCanvas );

				// set the variable to keep track of when to draw

				let paint = false;

				// add canvas event listeners
				drawingCanvas.addEventListener( 'pointerdown', function ( e ) {

					paint = true;
					drawStartPos.set( e.offsetX, e.offsetY );

				} );

				drawingCanvas.addEventListener( 'pointermove', function ( e ) {

					if ( paint ) draw( drawingContext, e.offsetX, e.offsetY );

				} );

				drawingCanvas.addEventListener( 'pointerup', function () {

					paint = false;

				} );

				drawingCanvas.addEventListener( 'pointerleave', function () {

					paint = false;

				} );

			}

			function draw( drawContext, x, y ) {

				drawContext.moveTo( drawStartPos.x, drawStartPos.y );
				drawContext.strokeStyle = '#000000';
				drawContext.lineTo( x, y );
				drawContext.stroke();
				// reset drawing start position to current position.
				drawStartPos.set( x, y );
				// need to flag the map as needing updating.
				material.map.needsUpdate = true;

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				mesh.rotation.x += 0.01;
				mesh.rotation.y += 0.01;

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
